package com.example.smart_city_0829.activity.youth_lnn.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.entity.YouthInn.InnList;
import com.example.smart_city_0829.utils.Constant;

import java.util.List;
import java.util.stream.Collectors;

public class InnExpandableListAdapter extends BaseExpandableListAdapter {

    private List<InnList.RowsBean> groupBeans;
    private List<String> introduces;

    private OnExpandButtonClickListener listener;

    public InnExpandableListAdapter(List<InnList.RowsBean> groupBeans,OnExpandButtonClickListener listener) {
        this.groupBeans = groupBeans;
        introduces = groupBeans.stream().map(InnList.RowsBean::getIntroduce).collect(Collectors.toList());
        this.listener = listener;
    }

    @Override
    public int getGroupCount() {
        return groupBeans.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupBeans.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return introduces.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.inn_group_item,null,false);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
            viewHolder.expandBt = (Button) convertView.findViewById(R.id.expand_bt);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }
        final InnList.RowsBean rowsBean = groupBeans.get(groupPosition);
        Glide.with(parent.getContext())
                .load(Constant.BASE_URL + rowsBean.getCoverImgUrl())
                .into(viewHolder.im);
        viewHolder.name.setText(rowsBean.getName());
        viewHolder.tag1.setText("剩余男床位数："+rowsBean.getBedsCountBoy());
        viewHolder.tag2.setText("剩余女床位数："+rowsBean.getBedsCountGirl());
        viewHolder.tag3.setText("详细地址："+rowsBean.getAddress());

        viewHolder.expandBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    Log.e("我又不动了","");
                    listener.onClick(groupPosition, isExpanded, parent);
                }
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView viewHolder = null;
        if (convertView == null){
            viewHolder = new TextView(parent.getContext());
            convertView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_expandable_list_item_1,null,false);
        }else {
            viewHolder = (TextView) convertView;
        }

        viewHolder.setText(groupBeans.get(groupPosition).getIntroduce());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private final static class GroupViewHolder{
        ImageView im;
        TextView name;
        TextView tag1;
        TextView tag2;
        TextView tag3;
        Button expandBt;

    }

    private final static class ChildViewHolder{

    }

    public interface OnExpandButtonClickListener{
        void onClick(int groupPosition, boolean isExpanded, ViewGroup parent);
    }
}
